package com.czh.springboot04.service.serviceimpl;

import com.czh.springboot04.entity.Appointment;
import com.czh.springboot04.entity.Consult;
import com.czh.springboot04.entity.User;
import com.czh.springboot04.mapper.AppointmentMapper;
import com.czh.springboot04.mapper.ConsultMapper;
import com.czh.springboot04.mapper.UserMapper;
import com.czh.springboot04.service.AppointmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppointmentServiceImpl implements AppointmentService {

    @Autowired
    AppointmentMapper appointmentMapper;
    @Autowired
    ConsultMapper consultMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public int insertAppointment(Appointment appointment) {
        return appointmentMapper.insertAppointment(appointment);
    }

    @Override
    public int updateAppointmentById(Appointment appointment) {
        return appointmentMapper.updateAppointmentById(appointment);
    }

    @Override
    public Appointment selectAppointmentById(Integer id) {
        return appointmentMapper.selectAppointmentById(id);
    }

    @Override
    public PageInfo<Appointment> selectAppointmentAll(Integer consultId, Integer userId, Integer status, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<Appointment> appointments = appointmentMapper.selectAppointmentAll(consultId, userId, status);
        for (Appointment appointment : appointments) {
            Consult consult = consultMapper.findConsultById(appointment.getConsultId());
            appointment.setConsult(consult);
            User user = userMapper.selectById(appointment.getUserId());
            appointment.setUser(user);
        }
        return new PageInfo<>(appointments);
    }

    @Override
    public int deleteAppointmentById(Integer id) {
        return appointmentMapper.deleteAppointmentById(id);
    }

    @Override
    public int deleteAppointmentByList(List<Integer> idList) {
        return appointmentMapper.deleteAppointmentByList(idList);
    }
}
